<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/9/24
  Time: 15:15
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="insert" method="post">
<div>
 分类： <select name="classId" id="cname">
        <option value="0">请选择分类</option>
 </select>
</div>
<div>
标题:<input type="text" name="title" id="title"/> <span id="sp"></span>
</div>
<div>
内容:<input type="text" name="content" id="content"/>
</div>
</form>
<button id="btn">保存</button> <a href="index.jsp">返回</a>
</body>
<script src="js/jquery-1.9.1.min.js"></script>
<script>
    $(function () {
        bindClasses()
        bindbtn()
        bindEved()
    })
    function bindbtn() {
        $("#btn").click(function () {
            var cname=$("#cname").val()
            if(cname==null || cname==""){
                alert("分类不能为空")
                return
            }var content=$("#content").val()
            if(cname==null || content==""){
                alert("内容不能为空")
                return
            }
            var title=$("#title").val()
            if(title==null || title==""){
                alert("标题不能为空")
                return
            }
            document.forms[0].submit();
        })

    }
    function bindClasses() {
        $.post("listClasses",function (msg) {
            var str=""
            $(msg).each(function (i) {
                var c=msg[i]
                str+="<option value='"+c.id+"'>"+c.cname+"</option>"
            })
            $("#cname").append(str)
        },"json")
    }
    function bindEved() {
        $("#title").blur(function () {
            var title=$("#title").val()
            if(title==null || title==""){
                alert("标题不能为空")
                return
            }
            $.post("count",{"title":title},function (msg) {
                if (msg.count==0){
                    $("#sp").html("可用")
                }else {
                    $("#sp").html("不可用")
                }
            },"json")
        })
    }
</script>
</html>
